package com.chencq.controller;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.chencq.annotation.WebInitParam;
import com.chencq.annotation.WebServlet;
import com.chencq.entity.Company;
import com.chencq.entity.User;
import com.chencq.util.ClassReflectUtil;

@WebServlet(
		value = "/LoginServlet", 
		urlPatterns = { "/LoginServlet" },
		initParams = { 
				@WebInitParam(paramName = "test1", paramValue = "aaa"), 
				@WebInitParam(paramName = "test2", paramValue = "bbbb") 
				},
		name = "LoginServlet",
		description = "处理用户登录的Servlet"
)
public class LoginServlet {

	private Logger  LOGGER=LoggerFactory.getLogger(LoginServlet.class);
	/**
	 * 一个servlet有多个请求处理方法 : 1.可以有多个请求处理方法 2.简化了转发和重定向的代码
	 * 
	 * 请求处理方式的格式 1. public String regist(HttpServletRequest request,
	 * HttpServletResponse response) throws ServletException, IOException { }
	 * 
	 * 访问的地址 http://localhost:8080/mvc/LoginServlet!login.do
	 * return "f:/view/test.jsp"; //f前缀表示forward，即转发view/test.jsp</br>
	 * return  "/view/test.jsp"; //即转发view/test.jsp </br>
	 * return "r:/view/test.jsp";//r前缀表示redirect，即重定向view/test.jsp </br>
	 * return null;//不转发，也不重定向 </br>
	 * return  "";//不转发，也不重定向</br>
	 */
	public String regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 LOGGER.info("regist()....");

		User user = (User) ClassReflectUtil.getEnity(User.class, request);
		Company company = (Company) ClassReflectUtil.getEnity(Company.class, request);
		 LOGGER.info(user.toString() + "--->" + company.toString());
		return "r:/view/test.jsp"; // 表示转发到view/test.jsp

	}

	public String login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 LOGGER.info("login()....");
		return "f:/view/test.jsp";
	}

	// 初始化方法
	public void init(Map<String, String> initParamMap) {
		 LOGGER.info("--LoginServlet初始化--");
		 LOGGER.info(initParamMap.get("test1"));
		 LOGGER.info(initParamMap.get("test2"));
	}
}
